//
//  BaseLoadingLayer.cpp
//  zoudasmallworld
//
//  Created by 周俊 on 14-6-13.
//
//

#include "BaseLoadingLayer.h"

USING_NS_CC;
TSP_NS_START

///////////////////////////////////////////////////////////////////////////////
BaseLoadingLayer::BaseLoadingLayer()
    :m_threadPool(nullptr)
{
}

BaseLoadingLayer::~BaseLoadingLayer()
{
    TSP_SAFE_DELETE(m_threadPool);
}

BaseLoadingLayer* BaseLoadingLayer::create()
{
    BaseLoadingLayer* layer = new BaseLoadingLayer();
    if ( layer && !layer->init() )
    {
        delete layer;
        layer = nullptr;
    }
    return layer;
}

bool BaseLoadingLayer::init()
{
    if ( !TSP::BaseLayer::init() )
    {
        return false;
    }

    m_threadPool = new ThreadPool(1);
    return true;
}

void BaseLoadingLayer::resetProgress()
{
    m_threadPool->reset();
}

void BaseLoadingLayer::completeLoading()
{
    emit(kOnComplete, this);
}

void BaseLoadingLayer::update(float delta)
{
    if (m_threadPool->getMaxTaskNum() == m_threadPool->getCurrentFinishedNum())
    {
        unscheduleUpdate();
        this->completeLoading();
    }
    return;
}

void BaseLoadingLayer::onEnter()
{
    return TSP::BaseLayer::onEnter();
}

void BaseLoadingLayer::onExit()
{
    return TSP::BaseLayer::onExit();
}

TSP_NS_END